//
//  XMLGenerator.cxx
//  Schedule
//
//  Created by Julius Canute on 2/16/12.
//  Copyright 2012 ABB. All rights reserved.
//

#include "XMLGenerator.h"

using namespace xercesc;
using namespace std;

DOMElement* XMLGenerator::addElement(DOMElement *parent,const char *tagName,const char *tagText,bool flag,const char *attrib, const char *value){
    DOMElement *child = NULL;
    DOMText *childTextNode;
    if (parent != NULL && tagName != NULL) {
        XMLString::transcode(tagName, tempStr, XMLSTRLEN - 1);
        child = doc->createElement(tempStr);
        parent->appendChild(child);
    }
    
    
    if (tagText != NULL) {
        XMLString::transcode(tagText, tempStr, XMLSTRLEN - 1);
        childTextNode = doc->createTextNode(tempStr);
        child->appendChild(childTextNode);
    }
    
    //Attribute and value must be specified for this to work
    if (attrib != NULL && value != NULL) {
        //Flag 'false' add the attribute to the parent or add the attribute to the 'child'.
        XMLString::transcode(attrib, tempStr, XMLSTRLEN - 1);
        DOMAttr *fieldNode = doc->createAttribute(tempStr);
        XMLString::transcode(value, tempStr, XMLSTRLEN - 1);
        fieldNode->setValue(tempStr);
        
        if (!flag) 
            parent->setAttributeNode(fieldNode);
        else
            child->setAttributeNode(fieldNode);
    }
    
    return child;
}

int XMLGenerator::SerializeToConsole(){
    
    DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
    XMLFormatTarget *myFormTarget = new StdOutFormatTarget();
    DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
    theOutput->setByteStream(myFormTarget);
    
    try {
        // do the serialization through DOMLSSerializer::write();
        theSerializer->write(root, theOutput);
    }
    catch (const XMLException& toCatch) {
        char *message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"<< message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (const DOMException& toCatch) {
        char *message = XMLString::transcode(toCatch.msg);
        cout << "Exception message is: \n"<< message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (...) {
        cout << "Unexpected Exception \n" ;
        return -1;
    }
    
    theOutput->release();
    theSerializer->release();
    delete myFormTarget;
    return 0;

}

int XMLGenerator::SerializeToFile(const char *name){
    XMLString::transcode(name, tempStr, XMLSTRLEN - 1);
    DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
    XMLFormatTarget *myFormTarget = new LocalFileFormatTarget(tempStr);
    DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
    theOutput->setByteStream(myFormTarget);
    
    if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true))
        theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
    
    if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
        theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
    
    try {
        // do the serialization through DOMLSSerializer::write();
        theSerializer->write(root, theOutput);
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"<< message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (const DOMException& toCatch) {
        char* message = XMLString::transcode(toCatch.msg);
        cout << "Exception message is: \n"<< message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (...) {
        cout << "Unexpected Exception \n" ;
        return -1;
    }
    
    theOutput->release();
    theSerializer->release();
    delete myFormTarget;
    return 0;
}
